package com.turing.pre.processor.file.processor;

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import lombok.extern.slf4j.Slf4j;

import java.io.*;

@Slf4j
public class Document4jApp {

    public static void main(String[] args) {
        String inputFilePath = "C:\\Users\\zhiqiang\\Desktop\\项目\\南京银行\\000847_中融货币市场基金2021年第三季度报告(1).doc";
        String pdfFilePath = "C:\\Users\\zhiqiang\\Desktop\\项目\\南京银行\\000847_中融货币市场基金2021年第三季度报告(1)_document4j3.pdf";
        word2PDF(inputFilePath,pdfFilePath);
    }

    public static void word2PDF(String inputFilePath, String pdfFilePath) {
        log.info("====================>word2PDF===by Document4j");
        File inputWord = new File(inputFilePath);
        File outputFile = new File(pdfFilePath);
        InputStream docxInputStream = null;
        OutputStream outputStream = null;
        try {
            docxInputStream = new FileInputStream(inputWord);
            outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            log.info("====================>word2PDF===success");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                docxInputStream.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }


}